package com.lxb.generate.common;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author lxb
 * @Date 2021/3/4 10:25
 * @Version 1.0
 */
public class R<T> extends HashMap<String, Object> {


    public R() {
        put("code", 200);
        put("msg", "success");
    }

    public static R ok() {
        return new R();
    }

    public static R ok(Object data) {
        R r = new R();
        r.put("data", data);
        return r;
    }

    public static R ok(Map<String, Object> map) {
        R r = new R();
        r.putAll(map);
        return r;
    }

    public static R ok(Object data,String msg) {
        R r = new R();
        r.put("data",data);
        r.put("msg",msg);
        return r;
    }

    public R put(String key, Object value) {
        super.put(key, value);
        return this;
    }

    public R put(Map<String,Object> map) {
        super.putAll(map);
        return this;
    }

    public static R error() {

        return error(500, "服务器错误");
    }

    public static R error(String msg) {
        return error(500, msg);
    }

    public static R error(Integer code, String msg) {
        R r = new R();
        r.put("code", code);
        r.put("msg", msg);
        return r;
    }

    public static R error(String msg,Object data) {
        R r = new R();
        r.put("code", 500);
        r.put("msg", msg);
        r.put("data",data);
        return r;
    }

    public static R error(Integer code) {
        R r = new R();
        r.put("code", code);
        return r;
    }

    public static R error(ResultEnum resultEnum) {
        R r = new R();
        r.put("code", resultEnum.getCode());
        r.put("msg",resultEnum.getMsg());
        return r;
    }

}
